import React,{useState} from 'react'
import axios from "axios";

export default function Login() {
  let  [img,setImg] = useState();

  let showImg = async ()=>{
    let  res  = await  axios({
      method:"get",
      url:"http://localhost:3003/login/qr/key",
      params:{
        timestamp:Date.now()
      }
    })
    let  key = res.data.data.unikey;

    let res2 = await  axios({
      method:"get",
      url:"http://localhost:3003/login/qr/create",
      params:{
        timestamp:Date.now(),
        key,
        qrimg:true
      }
    })
    let qrimg  = res2.data.data.qrimg;
    setImg(qrimg);

    setTimeout(async ()=>{//验证验证码
      let res3  = await  axios({
        method:"get",
        url:"http://localhost:3003/login/qr/check",
        params:{
          timestamp:Date.now(),
          key,
          noCookie:true
        }
      })
      console.log(res3);
      console.log(res3.data.cookie);
      if(res3.data.code ==803){
        document.cookie = res3.data.cookie;
      }

    },8000);
    


  }
  let  showInfo =async ()=>{
    let res  = await  axios({
      method:"get",
      url:"http://localhost:3003/login/status",
      withCredentials: true,
      params:{
        timestamp:Date.now(),
      }
    })
    console.log(res);

    let  res2 = await axios({
      method:"get",
      url:"http://localhost:3003/user/playlist",
      params:{
        uid:res.data.data.account.id
      }
    })
    console.log(res2);
  }

  return (
    <div>
      <div>扫码登录</div>
      <button onClick={()=>{
        showImg()
      }}>扫描</button>
      {img?<img  src={img}/>:""}

      <button onClick={()=>{
        showInfo()
      }}>查看个人信息</button>
      

    </div>
  )
}
